|
1
|
|
|
import path from 'path'; |
|
2
|
|
|
import jsonServer from 'json-server'; |
|
3
|
|
|
import fse from 'fs-extra'; |
|
4
|
|
|
import * as fixtures from './fixtures'; |
|
5
|
|
|
|
|
6
|
|
|
const filesDir = path.join(__dirname, './files'); |
|
7
|
|
|
|
|
8
|
|
|
const { users, actions } = fixtures; |
|
9
|
|
|
const badCode = 404; |
|
10
|
|
|
|
|
11
|
|
|
function createMockApp() { |
|
12
|
|
|
const server = jsonServer.create(); |
|
13
|
|
|
const router = jsonServer.router({ users }); |
|
14
|
|
|
const middlewares = jsonServer.defaults(); |
|
15
|
|
|
|
|
16
|
|
|
server.use(middlewares); |
|
17
|
|
|
|
|
18
|
|
|
server.start = async function (port) { |
|
19
|
|
|
let app; |
|
20
|
|
|
|
|
21
|
|
|
server.use('/api', router); |
|
22
|
|
|
server.post('/format/:format', async (req, res) => { |
|
23
|
|
|
if (req.params.format === 'xml') { |
|
24
|
|
|
res.type('application/xml'); |
|
25
|
|
|
|
|
26
|
|
|
return res.send('<status>OK</status>'); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
if (req.params.format === 'Buffer') { |
|
30
|
|
|
const content = await fse.readFile(path.join(filesDir, '1.txt')); |
|
31
|
|
|
|
|
32
|
|
|
res.writeHead(200, { |
|
33
|
|
|
'Content-Type' : 'mimetype', |
|
34
|
|
|
'Content-disposition' : 'attachment;filename=1.txt', |
|
35
|
|
|
'Content-Length' : content.length |
|
36
|
|
|
}); |
|
37
|
|
|
|
|
38
|
|
|
return res.end(Buffer.from(content, 'binary')); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
res.sendStatus(badCode); |
|
42
|
|
|
}); |
|
43
|
|
|
|
|
44
|
|
|
await new Promise(res => { |
|
45
|
|
|
app = server.listen(port, res); |
|
46
|
|
|
}); |
|
47
|
|
|
|
|
48
|
|
|
return app; |
|
49
|
|
|
}; |
|
50
|
|
|
|
|
51
|
|
|
return server; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
export default createMockApp; |
|
55
|
|
|
export { fixtures, actions }; |
|
56
|
|
|
|
|
57
|
|
|
|